<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form label-width="220px">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <upload-img ref="refUploadImg" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSubmit">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById, saveUserDetailById } from '@/api/user'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: ''
      }
    }
  },
  created() {
    this.loadUserDetailById()
  },
  methods: {
    async loadUserDetailById() {
      const res = await getUserDetailById(this.userId)
      this.userInfo = res.data

      // 1. 显示头像
      // 给子组件中imageUrl赋值
      // console.log('子组件是：', this.$refs.refUploadImg)
      this.$refs.refUploadImg.imageUrl = res.data.staffPhoto
    },
    async doSave() {
      // 2. 保存头像：从上传组件中获取图片上传到cos之后的地址，再调用接口做上传本项目的服务器
      this.userInfo.staffPhoto = this.$refs.refUploadImg.imageUrl
      const res = await saveUserDetailById(this.userInfo)
      this.$message.success('保存成功')
      console.log(res)
    },
    hSubmit() {
      // 1. 表单校验
      // 2. 发请求
      // this.$refs.userForm.validate(valid => {
      // if (valid) {
      this.doSave()
      // }
      // })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
